package evolution;

import hypergraph.HyperEdge;
import hypergraph.HyperGraph;
import hypergraph.Node;

import java.util.ArrayList;
import java.util.HashMap;


import org.jgap.FitnessFunction;
import org.jgap.IChromosome;

import bangor.aiia.jge.evolution.Genesis;
import bangor.aiia.jge.population.InvalidPhenotypeException;
import edu.uci.ics.jung.algorithms.scoring.BarycenterScorer;
import edu.uci.ics.jung.algorithms.shortestpath.Distance;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;

public class GraphMetricFitnessFunction extends FitnessFunction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5819833432480257564L;

	@Override
	protected double evaluate(IChromosome chrom) {
		boolean bigenough = false;
		//System.out.println("mapping...");
		HyperGraph g = new GraphMapper().map(chrom);

		double fitness = 0;
		//fitness = Math.max(0, 50 - Math.abs(50 - g.getVertexCount()));
		//System.out.println("fitness:"+fitness);
		/*
		DirectedSparseMultigraph<Node, HyperEdge> dsg = g.toGraph();
		Bary bary = new Bary(dsg);
		double total = 0;
		for(Node n : dsg.getVertices()){
			double s = bary.getVertexScore(n);
			//System.out.println(s);
			total += s;
		}*/
		//fitness += total / (float) (dsg.getVertexCount());
		fitness += Math.max(0, 30f - Math.abs(g.getVertexCount() - 30f)) * 2;
		fitness += Math.max(0, 65f - Math.abs(g.getEdgeCount() - 65f));
		//System.out.println("fitness" + fitness);
		return fitness;
	}
	
	public class Bary extends BarycenterScorer<Node, HyperEdge>{

		public Bary(Graph<Node, HyperEdge> graph) {
			super(graph);
			output = new HashMap<Node, Double>();
		}
		
	}
}
